/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    RackException.cs

Abstract: 

    This class defines the specific Exception for Rack Server Virtualization Add-In
*/

# region References
using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;

# endregion References

namespace UtilityManager
{
    /// <summary>
    /// Specific Exception Class for Rack Server Connector
    /// </summary>
    [SerializableAttribute]
    public class RackException : Exception
    {
        #region Construction

        /// <summary>
        /// Constructor
        /// </summary>
        public RackException()
            : base()
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public RackException(
			string errMsg)
			: // initializer list
            base(errMsg)
		{
		}

        /// <summary>
        /// Constructor
        /// </summary>
        public RackException(
            string errMsg, Exception ex)
            : // initializer list
            base(errMsg,ex)
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        protected RackException(SerializationInfo serialInfo, StreamingContext streamContext)
            : base(serialInfo, streamContext)
        {
        }

        #endregion

        #region Methods
        /// <summary>
        /// Method to handle Exceptions thrown
        /// </summary>
        /// <param name="ex">Parameter to handle to the exception thown</param>
        static public void HandleExceptions(System.Exception ex)
        {
            if (ex == null)
                return;

            // Don't worry about any ThreadAbortExceptions.
            if (ex.GetType() == typeof(System.Threading.ThreadAbortException))
            {
                return;
            }
            return;
        }

        /// <summary>
        /// Generic Debug Operation for writing out Exceptions.
        /// This will display the exception no matter the build type.
        /// </summary>
        /// <param name="ex">
        /// </param>
        static public void HandleExceptions(
            string caption,
            string userMessage,
            Exception ex)
        {
            string exceptionDetails = string.Empty;

            if (ex == null)
            {
                // Exceptions details not present
                exceptionDetails = "";
            }
            else
            {
                if (ex.GetType() == typeof(System.Threading.ThreadAbortException))
                {
                    // Ignore ThreadAbortExceptions
                    return;
                }
                exceptionDetails = ex.ToString();
            }

            // show the user the details of the exception
            using (ExceptionForm exceptionForm = new ExceptionForm(
                       false,
                       caption,
                       userMessage,
                       exceptionDetails))
            {
                exceptionForm.ShowDialog();
            }// .Dispose() is called automatically

            // Default return
            return;
        }
        #endregion
    }
}
